/**
* 精灵类
* 选择支，菜单界面的定义
*
*/
var spriteManager = function () {
	var spriteList = {},	//精灵列表
		buttonList = [],	//按钮列表
		sp = $('#sprite')[0].getContext('2d'), //精灵绘画环境
		spt = $('#spritetext')[0].getContext('2d'),
		menuVisible = false,
		fontSize = 25,				// 菜单字体大小
		font = '微软雅黑',			// 菜单字体
		fontColor = '#BBB',			// 字体颜色
		overColor = '#FFF',			// 鼠标悬停字体颜色
		menuColor = '#000000',		// 菜单颜色
		menuBGAlpha = 0.5,
		menuImage = new Image();
		// 增加精灵到精灵列表中
		var addsprite = function (id) {
			
		},
		// 增加按钮到按钮列表中
		addButton = function (text, x, y, btn, width, height){
			var btnObj = {};
				btnObj.x = x;
				btnObj.y = y;
				btnObj.text = text;
			// 	未提供高和宽则根据字符长度生成
			if (!width || !height) {
				sp.save();
				sp.font = fontSize+"px"+" "+font;
				btnObj.width = sp.measureText(text).width;
				btnObj.height = fontSize + 4;
				sp.restore();
			} else {
				btnObj.width = width;
				btnObj.height = height;
			}
			buttonList[btn] = btnObj;
		}
		// 改变按钮文字颜色
		changeBtnTextColor = function (btn, type) {
			sp.font = fontSize+"px"+" "+font;
			sp.textAlign = "start";		
			sp.textBaseline = "top";
			var x = buttonList[btn].x,
				y = buttonList[btn].y,
				text = buttonList[btn].text,
				width = buttonList[btn].width,
				height = buttonList[btn].height;
			if (type == 'over'){
				spt.clearRect(x, y, width, height);
				spt.fillStyle = overColor;
				spt.fillText(text, x, y);
			} else {
				// 这个判断是解决如果鼠标在某个选项上时退出菜单会导致触发一次mouseout事件，导致该选项未被正确清除
				if (sys.getStatus() == 'menu') {
					spt.clearRect(x, y, width, height);
					spt.fillStyle = fontColor;
					spt.fillText(text, x, y);
				}
			}
			
		},
		// 绘制菜单
		drawMenu = function (type, src) {
			//sp.globalAlpha = 0.5;
			switch(type){
				// 菜单背景为单色
				case 'color':
					sp.save();
					sp.globalAlpha = menuBGAlpha;
					sp.fillStyle = menuColor;
					sp.fillRect(0, 0, 800, 600);
					sp.restore();
					drawSelectText();
					break;
				// 菜单背景为图片
				case 'image':
					if (menuImage.src != undefined){
						sp.save();
						sp.fillStyle = menuColor;
						sp.globalAlpha = menuBGAlpha;
						sp.fillRect(0, 0, 800, 600);
						sp.drawImage(menuImage, 0, 0, 800, 600);
						sp.restore();
						drawSelectText();
					};
					menuImage.src = src;
					if (menuImage != undefined){
						menuImage.onload = function() {
							sp.save();
							sp.fillStyle = menuColor;
							sp.globalAlpha = menuBGAlpha;
							sp.fillRect(0, 0, 800, 600);
							sp.drawImage(menuImage, 0, 0, 800, 600);
							sp.restore();
							drawSelectText();
						};
					}
					else{
						alert('Can\'t load image...');
					}
					break;
				default:
					alert('Unknown type of menu background');
					break;
			};
		},
		// 绘制菜单文字
		drawSelectText = function () {
			// 预先写入菜单按钮
			addButton('保存文件',350,200,'save');
			addButton('读取文件',350,230,'load');
			addButton('游戏设置',350,260,'option');
			addButton('回忆模式',350,290,'back');
			addButton('退出游戏',350,320,'exit');
			sm.drawBtn('save');
			sm.drawBtn('load');
			sm.drawBtn('option');
			sm.drawBtn('back');
			sm.drawBtn('exit');
		},
		that = {
			// 新建一个精灵
			newSp: function (id, width, height, src) {
				var id,
					x = 0,
					y = 0,
					width = width,
					height = height,
					image = new Image(),
					offsetX = 0,
					offsetY = 0,
					onClick = null,
					onMouseover = null,
					onMouseout = null,
					$top = $('#top').append('<div/>').find(':last'),
					topStyle = $top[0].style;
				$topStyle.css({
					position: 'absolute',
					left:-1000, // 初始位置要放到DIV之外，不然会有一个奇怪的东西在左上角闪
					width: width,
					height: height,
				});
				image.src = src;
			},
			// 新建一个单色精灵
			newSp2: function () {
			
			},
			// 删除精灵
			deleteSp: function () {
				
			},
			// 新建按钮
			newBtn: function (text, x, y, btn, width, height) {
				addButton(text, x, y, btn, width, height);
			},
			// 显示按钮
			drawBtn: function (btn) {
				var x = buttonList[btn].x,
					y = buttonList[btn].y,
					text = buttonList[btn].text;
				spt.font = fontSize+"px"+" "+font;
				spt.fillStyle = fontColor;
				spt.textAlign = "start";		
				spt.textBaseline = "top";
				spt.fillText(text, x, y);
				var width = buttonList[btn].width,
					height = buttonList[btn].height;
				$('#top').append('<div id=' + btn + ' onmouseover="sm.overBtn(this.id)" onmouseout="sm.outBtn(this.id)"/>' );
				$('#'+ btn).css({"position":"absolute", "left": x+'px',"top": y+'px',"width":width, "height":height});
			},
			// 隐藏某个按钮
			hideBtn: function (btn) {
				var x = buttonList[btn].x,
					y = buttonList[btn].y,
					width = buttonList[btn].width,
					height = buttonList[btn].height;
				sp.clearRect(x, y, width, height);
				spt.clearRect(x, y, width, height);
				$('#' + btn).remove();
			},
			//删除某个按钮
			deleteBtn: function (btn) {
				buttonList[btn] = undefined;
			},
			// 鼠标和某个按钮重叠时
			overBtn: function(btn){
				changeBtnTextColor(btn, "over");
			},
			// 鼠标离开某个按钮时
			outBtn: function(btn){
				changeBtnTextColor(btn, "out");
			},
			// 显示菜单
			showMenu: function () {
				if (!menuVisible) {
					drawMenu('image',"img/menubg.jpg");
					//drawMenu('color');
					sys.changeStatus('menu');
					menuVisible = true;
				}
				else{
					sys.changeStatus('waitclick');
					sp.clearRect(0, 0, 800, 600);
					spt.clearRect(0, 0, 800, 600);
					sm.hideBtn('save');
					sm.hideBtn('load');
					sm.hideBtn('option');
					sm.hideBtn('back');
					sm.hideBtn('exit');
					menuVisible = false;
				}
				
			}
		}
	return that;
}